package functions 
{	import functions.FunctionNode;
	/**	 * @author pbalogh	 */	public class DoTimesFunctionNode extends FunctionNode 
	{
		private static var instancenumber : int = 1;		public function DoTimesFunctionNode()
		{
			trace("Making dotimes");
			funcname = "dotimes";
			functiontype = MACRO;		}
		
		override public function getValue( args:XML, scope:Object ) : XML
		{
			// what you pass to dotimes is a pair of lists:
			// one defining your args, such as (i 10)
			// and the next is the code you want to execute that many times.
			
			var returnXML:XML = <list />;
			var argslist : XML = args.children()[0];
			
			var functionbodyxml : XML = args.children()[1];
			var varnamexml : XML = argslist.children()[ 0 ];
			var varvaluexml : XML = argslist.children()[ 1 ];
			
			var varnamestring : String = Evaluator.getLiteral( Evaluator.evaluate(varnamexml, scope) );
			
			var value : * = Evaluator.getLiteral( Evaluator.evaluate(varvaluexml, scope) );
			
			// make a new scope to enclose the variables in our argslist
			var newscope : Object = { parentscope : scope, scopename: "dotimesscope" + instancenumber++};
			
			for( var i : int = 1; i <= value; i++)
			{
				newscope[ "symbolvalue" + varnamestring ] = <numberliteral>{i}</numberliteral>;
			
				returnXML = Evaluator.evaluate( functionbodyxml, newscope );
			}
			
			return <list />;	
		}
	}}